/**   
 * @Title: BeidanAgp.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description:北单ActivityGroup
 * @author: Kael gaojian@500wan.com   
 * @date: 2012-4-6 上午9:32:09 
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.ActivityGroup;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.ExpectListInfo;
import com.esun.model.UserInfo;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.hemaicenteragp.BeidanshengpingfuHemaiListAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.beidanagp.BeidanChooseAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.beidanagp.BeidanChooseList;
import com.esun.rabbit2.viewer.mainact.lotteryinfoact.LotteryInfoListFootball;
import com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord.LotteryRecordBeiDanAct;
import com.esun.ui.R;
import com.esun.util.log.EDebug;
import com.esun.util.wheel.widget.NumericWheelAdapter;
import com.esun.util.wheel.widget.OnWheelChangedListener;
import com.esun.util.wheel.widget.OnWheelScrollListener;
import com.esun.util.wheel.widget.WheelView;

/**
 * @ClassName: BeidanAgp
 * @Description: 北单ActivityGroup
 * @author: Kael gaojian@500wan.com
 * @date: 2012-4-6 上午9:32:09
 * 
 */
public class BeidanAgp extends ActivityGroup implements OnClickListener, OnTabChangeListener {
	private TabHost BeiDanTabHostMain;
	private ImageButton BeiDanAgpCancle;// 取消
	private ImageButton BeiDanAgpMenu;// 菜单
	private ImageButton BeiDanAgpFilter;// 筛选
	private static final String TAB_CHOOSE = Config.NUM0;// 菜单-选号投注
	private static final String TAB_HEMAI = Config.NUM1;// 菜单-参与合买
	private static final String TAB_LOTTERY_RECORD = Config.NUM2;// 菜单-购彩记录
	private static final String TAB_LOTTERY_INFO = Config.NUM3;// 菜单-开奖信息
	private static final String TAB_INTRODUCTION = Config.NUM4;// 菜单-玩法介绍
	private static final int TAB_CHOOSELIST = 0;// 选号投注
	private static final int TAB_CANYUHEMAI = 1;// 参与合买
	private static final int TAB_LOTTERYRECORD = 2;// 购彩记录
	private static final int TAB_LOTTERYINFO = 3;// 开奖信息
	private static final int TAB_INTRODUCTIONPAGE = 4;// 玩法介绍
	private Dialog menuDialog = null;// 菜单Dialog
	private Button menuBtnChoose;// 选号投注
	private Button menuBtnHemai;// 参与合买
	private Button menuBtnRecord;// 购彩记录
	private Button menuBtnZhuihaoRecord;// 追号记录，北单没有，不显示
	private Button menuBtnInfo;// 开奖信息
	private Button menuBtnIntroduction;// 玩法介绍
	private LinearLayout linearMenu;
	private TextView tvTitle;// 标题
	private int Current_Tab_Num;// 当前tab编号

	private Bundle bundleSetCurrentTab;
	public static Handler handlerBeidanAgp;// 控制标题及筛选按钮的Handler
	public static final int IN_CHOOSE_VIEW = 10001;// 选号页面
	public static final int IN_LIST_VIEW = 10002;// 投注列表
	public static final int SHOW_FILTER = 10003;// 已经筛选过
	public static final int NO_FILTER = 10004;// 没有筛选
	private Button btnExpectChoose;// 日期选择

	private final String MSG_EXPCTGETFAILED = "期号获取失败";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.beidan_agp);
		bundleSetCurrentTab = this.getIntent().getExtras();
		Current_Tab_Num = bundleSetCurrentTab.getInt("Current_Tab_Num");
		initHandlerUI();
		initUI();
	}

	/**
	 * 初始化TabHost
	 */
	private void initTabHost() {
		BeiDanTabHostMain = (TabHost) findViewById(R.id.beidanagp_tabhost_main);
		BeiDanTabHostMain.setup(BeidanAgp.this.getLocalActivityManager());
		// 选队投注
		TabSpec ts0 = BeiDanTabHostMain.newTabSpec(TAB_CHOOSE);
		ts0.setIndicator(Config.NULLMARK);
		ts0.setContent(new Intent(this, BeidanChooseAct.class));
		BeiDanTabHostMain.addTab(ts0);
		// 参与合买
		TabSpec ts1 = BeiDanTabHostMain.newTabSpec(TAB_HEMAI);
		ts1.setIndicator(Config.NULLMARK);
		ts1.setContent(new Intent(this, BeidanshengpingfuHemaiListAct.class));
		BeiDanTabHostMain.addTab(ts1);
		// 北单购彩记录
		TabSpec ts2 = BeiDanTabHostMain.newTabSpec(TAB_LOTTERY_RECORD);
		ts2.setIndicator(Config.NULLMARK);
		ts2.setContent(new Intent(this, LotteryRecordBeiDanAct.class));
		BeiDanTabHostMain.addTab(ts2);
		// 北单开奖信息
		TabSpec ts3 = BeiDanTabHostMain.newTabSpec(TAB_LOTTERY_INFO);
		ts3.setIndicator(Config.NULLMARK);
		ts3.setContent(new Intent(this, LotteryInfoListFootball.class).putExtra(Key.KEY_LOTID, Config.BEIJINGZUQIUDANCHANG));
		BeiDanTabHostMain.addTab(ts3);
		// 北单玩法介绍
		TabSpec ts4 = BeiDanTabHostMain.newTabSpec(TAB_INTRODUCTION);
		ts4.setIndicator(Config.NULLMARK);
		ts4.setContent(new Intent(this, HelpAct.class).putExtra(Key.KEY_URL, Config.BEIDAN_INTRODUCE_WEB_ADD).putExtra(Key.KEY_TITLE, Config.NULLMARK).putExtra(Key.KEY_HAVETITLE, Config.NUM0));
		BeiDanTabHostMain.addTab(ts4);

		BeiDanTabHostMain.setCurrentTab(Current_Tab_Num);
		BeiDanTabHostMain.setOnTabChangedListener(this);
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {
		initTabHost();
		BeiDanAgpCancle = (ImageButton) findViewById(R.id.beidanagp_btn_cancel);
		BeiDanAgpCancle.setOnClickListener(this);
		BeiDanAgpMenu = (ImageButton) findViewById(R.id.beidanagp_btn_menu);
		BeiDanAgpMenu.setOnClickListener(this);
		BeiDanAgpFilter = (ImageButton) findViewById(R.id.beidanagp_btn_filter);
		BeiDanAgpFilter.setOnClickListener(this);
		btnExpectChoose = (Button) findViewById(R.id.beidan_agp_btn_expectChoose);
		btnExpectChoose.setOnClickListener(this);
		tvTitle = (TextView) findViewById(R.id.beidanagp_tv_title);
		if (Current_Tab_Num == 0) {
			tvTitle.setText(Language.LOTNAME_BEIDANSHENGPINGFU + "投注");// 北单胜平负
		} else if (Current_Tab_Num == 1) {
			BeiDanAgpFilter.setVisibility(View.GONE);
			tvTitle.setText(Language.MSG_BEIDAN_SHENGPINGFU_TOUZHU_LIST);// 北单胜平负投注列表
		}
	}

	/**
	 * 初始化控制标题及筛选按钮的Handler
	 */
	private void initHandlerUI() {
		handlerBeidanAgp = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				// 选队投注界面标题显示"北单胜平负",显示筛选按钮
				case IN_CHOOSE_VIEW:
					tvTitle.setText(Language.LOTNAME_BEIDANSHENGPINGFU + "投注");
					BeiDanAgpFilter.setVisibility(View.VISIBLE);
					break;
				// 投注列表界面标题显示"北单胜平负投注列表",筛选按钮隐藏
				case IN_LIST_VIEW:
					tvTitle.setText(Language.MSG_BEIDAN_SHENGPINGFU_TOUZHU_LIST);
					BeiDanAgpFilter.setVisibility(View.GONE);
					break;
				// 已经筛选过时筛选按钮的显示
				case SHOW_FILTER:
					BeiDanAgpFilter.setImageResource(R.drawable.icon_pressed_pickteam_sx_42x48);
					break;
				// 未筛选时筛选按钮的显示
				case NO_FILTER:
					BeiDanAgpFilter.setImageResource(R.drawable.icon_default_pickteam_sx_33x40);
					break;
				default:
					break;
				}
			}
		};
	}

	/**
	 * 设置菜单
	 */
	private void setMenuDialog() {
		// 菜单dialog
		menuDialog = new Dialog(this, R.style.dialogNoFloating);
		menuDialog.getWindow().setContentView(R.layout.shuangseqiuagp_dialog_menu);
		// 菜单-选号投注
		menuBtnChoose = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_choose);
		menuBtnChoose.setOnClickListener(this);
		// 菜单-参与合买
		menuBtnHemai = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_hemai);
		menuBtnHemai.setOnClickListener(this);
		// 菜单-购彩记录
		menuBtnRecord = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_lotteryRecord);
		menuBtnRecord.setOnClickListener(this);
		// 菜单-追号记录，北单没有所以不显示
		menuBtnZhuihaoRecord = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_zhuihaoRecord);
		menuBtnZhuihaoRecord.setVisibility(View.GONE);
		ImageView imgZhuihao = (ImageView) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_img_zhuihao);
		imgZhuihao.setVisibility(View.GONE);
		// 菜单-开奖信息
		menuBtnInfo = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_lotteryInfo);
		menuBtnInfo.setOnClickListener(this);
		// 菜单-玩法介绍
		menuBtnIntroduction = (Button) menuDialog.getWindow().findViewById(R.id.shuangseqiuagpdialogmenu_btn_introduction);
		menuBtnIntroduction.setOnClickListener(this);

		linearMenu = (LinearLayout) menuDialog.getWindow().findViewById(R.id.shuangseqiuagp_dialog_menu_linear_bg);
		linearMenu.setOnClickListener(this);
		menuDialog.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
			}
		});
		menuDialog.show();
	}

	@Override
	public void onTabChanged(String tabId) {
		int index = Integer.valueOf(tabId);
		// 判断是否显示期号选择按钮、只有开奖信息的时候显示选择期号按钮
		if (index == TAB_LOTTERYINFO) {
			btnExpectChoose.setVisibility(View.VISIBLE);
		} else {
			btnExpectChoose.setVisibility(View.GONE);
		}
		switch (index) {
		// 选队投注的两个界面的标题：选队界面显示"北单胜平负"，投注列表界面显示"被单胜平负投注列表"
		case TAB_CHOOSELIST:
			if (((SportteryChooseActivity) BeiDanTabHostMain.getCurrentView().getContext()).getCurrentPageFlag() == 0) {
				tvTitle.setText(Language.LOTNAME_BEIDANSHENGPINGFU + "投注");
				BeiDanAgpFilter.setVisibility(View.VISIBLE);
			} else if (((SportteryChooseActivity) BeiDanTabHostMain.getCurrentView().getContext()).getCurrentPageFlag() == 1) {
				tvTitle.setText(Language.MSG_BEIDAN_SHENGPINGFU_TOUZHU_LIST);
				BeiDanAgpFilter.setVisibility(View.GONE);
			}
			break;
		// 参与合买界面标题显示"北单胜平负合买列表"
		case TAB_CANYUHEMAI:
			tvTitle.setText(Language.MSG_BEIDAN_SHENGPINGFU_HEMAI_LIST);
			break;
		// 购彩记录列表界面标题显示"北单胜平负购彩记录"
		case TAB_LOTTERYRECORD:
			tvTitle.setText(Language.MSG_BEIDAN_SHENGPINGFU_LOTTERY_RECORD);
			break;
		// 开奖信息界面标题显示"北单胜平负开奖信息"
		case TAB_LOTTERYINFO:
			tvTitle.setText(Language.MSG_BEIDAN_SHENGPINGFU_LOTTERY_INFO);
			break;
		// 玩法介绍界面标题显示"北单胜平负玩法介绍"
		case TAB_INTRODUCTIONPAGE:
			tvTitle.setText(Language.MSG_BEIDAN_SHENGPINGFU_PLAY_INTRODUCTION);
			break;

		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {
		Message msgRefresh = new Message();
		msgRefresh.what = Config.DO_REFRESH;

		switch (v.getId()) {
		// 取消
		case R.id.beidanagp_btn_cancel:
			BeidanAgp.this.finish();
			break;
		// 菜单
		case R.id.beidanagp_btn_menu:
			setMenuDialog();
			break;
		// 筛选
		case R.id.beidanagp_btn_filter:
			Message msg = BeidanChooseList.handlerChoosePage.obtainMessage(BeidanChooseList.DO_FILTER, null);
			BeidanChooseList.handlerChoosePage.sendMessage(msg);
			break;
		// 选号投注
		case R.id.shuangseqiuagpdialogmenu_btn_choose:
			BeiDanTabHostMain.setCurrentTab(0);
			menuDialog.dismiss();
			break;
		// 参与合买
		case R.id.shuangseqiuagpdialogmenu_btn_hemai:
			BeiDanAgpFilter.setVisibility(View.GONE);
			BeiDanTabHostMain.setCurrentTab(1);
			menuDialog.dismiss();
			break;
		// 购彩记录
		case R.id.shuangseqiuagpdialogmenu_btn_lotteryRecord:
			// 查看是否已登陆,必须先登录才能看购彩记录
			if (isLogin()) {// 已经登陆
				BeiDanAgpFilter.setVisibility(View.GONE);
				BeiDanTabHostMain.setCurrentTab(2);
				LotteryRecordBeiDanAct.handlerGetBeiDanLotteryRecord.sendMessage(msgRefresh);
			} else {// 没有登录
				startActivity(new Intent(BeidanAgp.this, LoginAct.class));
			}
			menuDialog.dismiss();
			break;
		// 开奖信息
		case R.id.shuangseqiuagpdialogmenu_btn_lotteryInfo:
			BeiDanAgpFilter.setVisibility(View.GONE);
			BeiDanTabHostMain.setCurrentTab(3);
			menuDialog.dismiss();
			break;
		// 玩法介绍
		case R.id.shuangseqiuagpdialogmenu_btn_introduction:
			BeiDanAgpFilter.setVisibility(View.GONE);
			BeiDanTabHostMain.setCurrentTab(4);
			menuDialog.dismiss();
			break;
		// 菜单
		case R.id.shuangseqiuagp_dialog_menu_linear_bg:
			menuDialog.dismiss();
			break;
		// 期号选择
		case R.id.beidan_agp_btn_expectChoose:
			// 提示期号选择框
			if (LotteryInfoListFootball.isGetExpectSucceed) {
				if (expectChooseDialog == null) {
					showExpectChooseDialog();
				}
				expectChooseDialog.show();
			} else {
				Toast.makeText(BeidanAgp.this, MSG_EXPCTGETFAILED, Toast.LENGTH_SHORT);
			}
			break;

		case R.id.dialog_expect_choose_btn_cancle:

			expectChooseDialog.cancel();

			break;

		case R.id.dialog_expect_choose_btn_submit:

			EDebug.o(this, "点击确定，应该是加载新一期的开奖信息");
			expectChooseDialog.cancel();
			Message msgExpect;
			msgExpect = LotteryInfoListFootball.handler.obtainMessage(LotteryInfoListFootball.EXPECT_CHANGED, expectSelected);
			LotteryInfoListFootball.handler.sendMessage(msgExpect);

			break;
		default:
			break;
		}

	}

	/**
	 * 按键响应
	 * 
	 * @return
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			setMenuDialog();// 设置菜单
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		return true;
	}

	/**
	 * 验证用户是否已登录
	 * 
	 * @return
	 */
	private boolean isLogin() {
		if (UserInfo.getUserInfo().getUserSession().length() > 0) {
			return true;
		} else {
			return false;
		}
	}

	// 被选中的期号
	private String expectSelected = "";
	private Dialog expectChooseDialog;
	private WheelView mWheelView;

	private void showExpectChooseDialog() {
		expectChooseDialog = new Dialog(BeidanAgp.this, R.style.dialog);
		expectChooseDialog.getWindow().setContentView(R.layout.dialog_expect_choose);

		Button btnSubmit = new Button(BeidanAgp.this);
		btnSubmit = (Button) expectChooseDialog.findViewById(R.id.dialog_expect_choose_btn_submit);
		btnSubmit.setOnClickListener(this);
		Button btnCancle = new Button(BeidanAgp.this);
		btnCancle = (Button) expectChooseDialog.findViewById(R.id.dialog_expect_choose_btn_cancle);
		btnCancle.setOnClickListener(this);

		final ArrayList<Hashtable<String, String>> expectList = new ArrayList<Hashtable<String, String>>();
		// Hashtable<String, String> mapTop = new Hashtable<String, String>();
		// mapTop.put("expect", "开始");
		// expectList.add(mapTop);
		for (int i = 1; i < ExpectListInfo.getExpectListInfo().getExpectList().size(); i++) {
			Hashtable<String, String> map = new Hashtable<String, String>();
			map.put("expect", ExpectListInfo.getExpectListInfo().getExpectList().get(i));
			expectList.add(map);
		}
		// Hashtable<String, String> mapBottom = new Hashtable<String, String>();
		// mapBottom.put("expect", "结束");
		// expectList.add(mapBottom);
		expectSelected = ExpectListInfo.getExpectListInfo().getExpectList().get(1);
		mWheelView = (WheelView) expectChooseDialog.findViewById(R.id.dialog_expect_choose_wheel_expect);
		mWheelView.setAdapter(new NumericWheelAdapter(ExpectListInfo.getExpectListInfo().getExpectList()));
		mWheelView.setCurrentItem(0);
		mWheelView.addChangingListener(changedListener);
		mWheelView.addScrollingListener(scrolledListener);
		mWheelView.setCyclic(true);
		mWheelView.setVisibleItems(5);
		mWheelView.setInterpolator(new AnticipateOvershootInterpolator());

	}

	// Wheel scrolled flag
	private boolean wheelScrolled = false;

	// Wheel changed listener
	private OnWheelChangedListener changedListener = new OnWheelChangedListener() {
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			if (!wheelScrolled) {
				updateStatus();
			}
		}
	};

	// Wheel scrolled listener
	OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
		public void onScrollingStarted(WheelView wheel) {
			wheelScrolled = true;
		}

		public void onScrollingFinished(WheelView wheel) {
			wheelScrolled = false;
			updateStatus();
		}
	};

	/**
	 * Updates entered PIN status
	 */
	private void updateStatus() {

		expectSelected = ExpectListInfo.getExpectListInfo().getExpectList().get(mWheelView.getCurrentItem() + 1);
		// Toast.makeText(LotteryInfoListAgp.this, "" +
		// ExpectListInfo.getExpectListInfo().getExpectList().get(mWheelView.getCurrentItem()
		// + 1), Toast.LENGTH_SHORT).show();

	}

}
